package geekstudy.week9;

import java.util.HashMap;

/**
 *
 *
 * 36. 有效的数独
 * https://leetcode-cn.com/problems/valid-sudoku/
 *
 *
 * 执行结果：  通过  显示详情
 *
 * 添加备注
 * 执行用时：3 ms, 在所有 Java 提交中击败了43.58% 的用户
 * 内存消耗：38.5 MB, 在所有 Java 提交中击败了55.28% 的用户
 *
 *
 */
public class Week9ValidSudoku {

    public boolean isValidSudoku(char[][] board) {
        HashMap<Integer, Integer>[] rowMapArray = new HashMap[9];
        HashMap<Integer, Integer>[] columnMapArray = new HashMap[9];
        HashMap<Integer, Integer>[] boxMapArray = new HashMap[9];
        for(int size = 0; size < 9; size++) {
            rowMapArray[size] = new HashMap<Integer, Integer>();
            columnMapArray[size] = new HashMap<Integer, Integer>();
            boxMapArray[size] = new HashMap<Integer, Integer>();
        }

        for(int rowSize = 0; rowSize < 9; rowSize++) {
            for(int columnSize = 0; columnSize < 9; columnSize++) {
                char num = board[rowSize][columnSize];
                if(num != '.') {
                    int n = (int)num;
                    int boxIndex = (rowSize / 3) * 3 + columnSize / 3;

                    rowMapArray[rowSize].put(n, rowMapArray[rowSize].getOrDefault(n, 0 ) + 1);
                    columnMapArray[columnSize].put(n, columnMapArray[columnSize].getOrDefault(n, 0) + 1);
                    boxMapArray[boxIndex].put(n, boxMapArray[boxIndex].getOrDefault(n, 0) + 1);

                    if(rowMapArray[rowSize].get(n) > 1 || columnMapArray[columnSize].get(n) > 1 || boxMapArray[boxIndex].get(n) > 1) {
                        return false;
                    }
                }
            }

        }

        return true;
    }
}
